package com.edu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.dto.SchoolCountStuDto;
import com.edu.entity.School;
import com.edu.entity.StuUpInfo;
import com.edu.mapper.SchoolMapper;
import com.edu.mapper.StuUpInfoMapper;
import com.edu.service.StuUpInfoService;
import com.edu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: graduate
 * @description: 预报名信息
 * @author: K.faWu
 * @create: 2021-05-23 14:00
 **/
@Service
public class StuUpInfoServiceImpl extends ServiceImpl<StuUpInfoMapper, StuUpInfo> implements StuUpInfoService {

    @Autowired
    private StuUpInfoMapper stuUpInfoMapper;
    @Override
    public String add(StuUpInfo stuUpInfo) {
        QueryWrapper<StuUpInfo> stuUpInfoQueryWrapper = new QueryWrapper<>();
        stuUpInfoQueryWrapper.eq("del_flag", 0);//未删除
        stuUpInfoQueryWrapper.eq("stu_id", stuUpInfo.getStuId());//学生是否已报名
        stuUpInfoQueryWrapper.eq("up_year",newYear());// 且是在今年
        List<StuUpInfo> stuUpInfoList = stuUpInfoMapper.selectList(stuUpInfoQueryWrapper);
        if(!CollectionUtils.isEmpty(stuUpInfoList)){
            return "您已今年已预报名本校，请勿多次报名！";
        }
        //填写信息
        stuUpInfo.setProcessIds("1");
        stuUpInfo.setUpYear(newYear());
        stuUpInfo.setDelFlag(0);
        /**
         * 预报名无考生号
         * 考生号在正式报名后该校考生统一生成
         */
        return stuUpInfoMapper.insert(stuUpInfo) == 1?"":"预报名失败";
    }

    /*
     * 获取当前年份
     */
    public String newYear(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(new Date());
    }

    @Override
    public Long upCandidate(Integer schoolId) {
        QueryWrapper<StuUpInfo> stuUpInfoQueryWrapper = new QueryWrapper<>();
        stuUpInfoQueryWrapper.eq("school_id", schoolId);
        stuUpInfoQueryWrapper.eq("up_year", newYear());
        stuUpInfoQueryWrapper.eq("process_ids", 2); //需要是正式报名后的学生
        stuUpInfoQueryWrapper.eq("del_flag", 0);
        long count = 0;
        List<StuUpInfo> stuUpInfoList = stuUpInfoMapper.selectList(stuUpInfoQueryWrapper);
        for(StuUpInfo stuUpInfo: stuUpInfoList){
            String upYear = stuUpInfo.getUpYear();
            String school = longToString(stuUpInfo.getSchoolId(), 4);
            String major = longToString(stuUpInfo.getMajorId(), 3);
            count += 1l;
            String num = longToString(count, 4);
            stuUpInfo.setCandidate(Long.decode(upYear + school + major + num));
            stuUpInfoMapper.updateById(stuUpInfo);
        }
        return count;
    }

    @Autowired
    private SchoolMapper schoolMapper;

    @Override
    public Result countByShoolList(List<Long> schoolList) {
        if (schoolList == null ||schoolList.size() == 0 ){
            for(long i = 1; i<=10; i++){
                schoolList.add(i);
            }
        }
        QueryWrapper<StuUpInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("del_flag", 0);
//        queryWrapper.eq("up_year",newYear());
        List<StuUpInfo> list = stuUpInfoMapper.selectList(queryWrapper);
        if(list == null || list.size() == 0){
            return Result.failure("", "今年尚未有人报名！");
        }
        Map<String, List> resutl = new HashMap<>();
        ArrayList<String> schoolNameList = new ArrayList<>();
        ArrayList<Long> forecastNumList = new ArrayList<>();
        ArrayList<Long> signUpNumList = new ArrayList<>();
        ArrayList<Long> adoptNumList = new ArrayList<>();
        ArrayList<Double> forecastPercentageList = new ArrayList<>();
        ArrayList<Double> signUpPercentageList = new ArrayList<>();
        ArrayList<Double>  adoptPercentageList = new ArrayList<>();
        List<School> schools = schoolMapper.selectList(new QueryWrapper<School>());
        for (long schoolId: schoolList) {
            //创建每个学校对应的信息
            Long forecastNum = 0L;
            double forecastPercentage;
            Long signUpNum = 0L;
            double signUpPercentage;
            Long adoptNum = 0L;
            double adoptPercentage;
            for (School school: schools){
                if(school.getId() == schoolId){
                    schoolNameList.add(school.getName());
                    break;
                }
            }
            for (StuUpInfo stuUpInfo : list) {
                if(stuUpInfo.getSchoolId() == schoolId){
                    if (StringUtils.equals(stuUpInfo.getProcessIds(), "4")){
                        //通过
                        adoptNum++;
                        signUpNum++;
                        forecastNum++;
                    }else if (StringUtils.equals(stuUpInfo.getProcessIds(), "2")){
                        //正式报名
                        signUpNum++;
                        forecastNum++;
                    }else if (StringUtils.equals(stuUpInfo.getProcessIds(), "1")){
                        //预报名
                        forecastNum++;
                    }

                }
            }
            forecastNumList.add(forecastNum);
            adoptNumList.add(adoptNum);
            signUpNumList.add(signUpNum);
            int size = list.size();
            forecastPercentage = forecastNum * 10000 /size/100D;
            adoptPercentage = adoptNum * 10000 /size/100D;
            signUpPercentage = signUpNum * 10000 /size/100D;
            forecastPercentageList.add(forecastPercentage);
            adoptPercentageList.add(adoptPercentage);
            signUpPercentageList.add(signUpPercentage);
        }
        resutl.put("学校",schoolNameList);
        resutl.put("预报名人数", forecastNumList);
        resutl.put("预报名人数占比",forecastPercentageList);
        resutl.put("确定报名人数", signUpNumList);
        resutl.put("确认报名人数占比",signUpPercentageList);
        resutl.put("通过人数", adoptNumList);
        resutl.put("通过人数占比",adoptPercentageList);
        return Result.success(resutl, list.size());
    }

    /**
     * 给出指定长度及指定值，生成对用长度、不足前置补零的字符串
     */
    private String longToString(long value, int length){
        String result = Long.toString(value);
        while (result.length() < length){
            result = "0" + result;
        }
        return result;
    }

    @Override
    public Result addStuInfoStandard(StuUpInfo stuUpInfo) {
        QueryWrapper<StuUpInfo> stuUpInfoQueryWrapper = new QueryWrapper<>();
        stuUpInfoQueryWrapper.eq("del_flag", 0);//未删除
        stuUpInfoQueryWrapper.eq("stu_id", stuUpInfo.getStuId());//学生是否已报名
        stuUpInfoQueryWrapper.eq("up_year",newYear());// 且是在今年
        stuUpInfoQueryWrapper.eq("process_ids",2);// 且是在今年
        List<StuUpInfo> stuUpInfoList = stuUpInfoMapper.selectList(stuUpInfoQueryWrapper);
        if(!CollectionUtils.isEmpty(stuUpInfoList)){
            return Result.failure("500","您已今年已预报名本校，请勿多次报名！");
        }
        //填写信息
        stuUpInfo.setProcessIds("2");
        stuUpInfo.setUpYear(newYear());
        stuUpInfo.setDelFlag(0);
        /**
         * 预报名无考生号
         * 考生号在正式报名后该校考生统一生成
         */
        stuUpInfoMapper.insert(stuUpInfo);
        return Result.success();
    }

}
